home *** CD-ROM | disk | FTP | other *** search
- Path: news.production.compuserve.com!news
- From: Peter Hammond <100443.2074@CompuServe.COM>
- Newsgroups: comp.lang.c++
- Subject: Re: returning an array from function
- Date: 4 Apr 1996 08:11:20 GMT
- Organization: Cygnus Intruments Ltd
- Message-ID: <4k0078$15m$2@mhadf.production.compuserve.com>
- References: <4jtr5h$h2s@nadine.teleport.com>
-
- I am getting really ((*$%รบ off with this post editor. Sometimes I
- can put in newlines, sometimes I cant.
- Anyway, another method which might work is:
-
- const int MaxN = 80;
- void ACaller (Void) {
- ...
- int N= // 1 to MAxN
- int* array = MakeMyArray (N);
- DoSomething (array[i]);
- }
-
- int* MakeMyArray (int N) {
- static int array[MaxN];
- // OR:
- int* array = new int[N];
- ...
- return array;
- }
-
- If MakeMyArray uses new, then ACaller MUST call delete (not
- necessarily directly) to get rid of it. This being C++, you could
- probably do something elegant with constructors/destructors.
- Otherwise, the array must be static so it is guaranteed to still
- exist after returning its pointer. I think.
-
- --
- Cygnus Instruments Ltd Ultrasonic NDT Equipment
- 30 Prince of Wales Rd, Dorchester, Dorset DT1 1PW
- England
- Tel +44 (0)1305 265533 Fax +44 (0)1305 269960
-